package com.zero.clinic.service.impl;

import com.zero.clinic.mapper.PatientDiagnoseMapper;
import com.zero.clinic.mapper.PatientRegisterMapper;
import com.zero.clinic.pojo.PatientDiagnose;
import com.zero.clinic.pojo.PatientDiagnoseExample;
import com.zero.clinic.pojo.PatientRegister;
import com.zero.clinic.service.PatientDiagnoseService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class PatientDiagnoseServiceImpl implements PatientDiagnoseService {

    @Resource
    private PatientDiagnoseMapper diagnoseMapper;

    @Resource
    private PatientRegisterMapper patientRegisterMapper;

    @Override
    public List<PatientDiagnose> selectByExample(PatientDiagnoseExample example) {
        return diagnoseMapper.selectByExample(example);
    }

    @Override
    public int deleteByPrimaryKey(Long diagnoseId) {
        // 1.先通过diagnoseId查询到当前patientDiagnose
        PatientDiagnose patientDiagnose = diagnoseMapper.selectByPrimaryKey(diagnoseId);
        // 2.再将patientDiagnose的patientRegisterId对应的patientRegister信息中的status由1改为0
        PatientRegister patientRegister = patientRegisterMapper.selectByPrimaryKey(patientDiagnose.getPatientRegisterId());
        patientRegister.setStatus(0L);
        patientRegisterMapper.updateByPrimaryKeySelective(patientRegister);
        // 3.最后将patientDiagnose删除
        return diagnoseMapper.deleteByPrimaryKey(diagnoseId);

    }

    @Override
    public int insert(PatientDiagnose patientDiagnose) {
        // 1.完成patientDiagnose的新增
        int insertResult = diagnoseMapper.insert(patientDiagnose);
        // 2.然后将其对应的patientRegister信息中的status由0改为1
        Long patientRegisterId = patientDiagnose.getPatientRegisterId();
        PatientRegister patientRegister = patientRegisterMapper.selectByPrimaryKey(patientRegisterId);
        patientRegister.setStatus(1L);
        patientRegisterMapper.updateByPrimaryKeySelective(patientRegister);

        return insertResult;
    }

    @Override
    public int insertSelective(PatientDiagnose record) {
        return diagnoseMapper.insertSelective(record);
    }

    @Override
    public PatientDiagnose selectByPrimaryKey(Long diagnoseId) {
        return diagnoseMapper.selectByPrimaryKey(diagnoseId);
    }

    @Override
    public int updateByPrimaryKeySelective(PatientDiagnose record) {
        return diagnoseMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int deleteByIds(String[] diagnoseIds) {
        // 1.遍历diagnoseIds
        for (String diagnoseId : diagnoseIds) {
            // 1.1 通过每一个diagnoseId获取到对应的patientDiagnose对象
            PatientDiagnose patientDiagnose = diagnoseMapper.selectByPrimaryKey(Long.parseLong(diagnoseId));
            // 1.2 再将这个patientDiagnose的patientRegisterId对应的patientRegister信息中的status由1改为0
            PatientRegister patientRegister = patientRegisterMapper.selectByPrimaryKey(patientDiagnose.getPatientRegisterId());
            patientRegister.setStatus(0L);
            patientRegisterMapper.updateByPrimaryKeySelective(patientRegister);
        }
        // 2.根据diagnoseIds批量删除就诊信息
        return diagnoseMapper.deleteByIds(diagnoseIds);
    }
}
